<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px" style="width: 400px; margin-left:50px;">
        <el-form-item label="type" prop="type">
          <el-input v-model="temp.type" />
      </el-form-item>
      <el-form-item label="lowestCode" prop="lowestCode">
          <el-input v-model="temp.lowestCode" />
      </el-form-item>
      <el-form-item label="lowestVersion" prop="lowestVersion">
          <el-input v-model="temp.lowestVersion" />
      </el-form-item>
      <el-form-item label="newestCode" prop="newestCode">
          <el-input v-model="temp.newestCode" />
      </el-form-item>
      <el-form-item label="newestVersion" prop="newestVersion">
          <el-input v-model="temp.newestVersion" />
      </el-form-item>
      <el-form-item label="title" prop="title">
          <el-input v-model="temp.title" />
      </el-form-item>
      <el-form-item label="content" prop="content">
          <el-input v-model="temp.content" />
      </el-form-item>
      <el-form-item label="directUrl" prop="directUrl">
          <el-input v-model="temp.directUrl" />
      </el-form-item>
      <el-form-item label="删除标志" prop="deleteflag">
          <select-option
              v-model="temp.deleteflag"
              :clearable=true
              prop-name="deleteflag"
              :prop-ds-type=1
          />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {createApi, updateApi} from './api'
// 导入多选项组件
import SelectOption from '@/views/coding/components/SelectOption'

export default {
    name: 'TmptAppVersion-FormDetail',

    components: { SelectOption },

    props: {
        isModule: {
            type: Boolean,
            default: false
        },
        row: {}
    },

    data() {
        return {
            rules: {
                type: [{ required: true, message: 'required', trigger: 'blur' }],
                lowestCode: [{ required: true, message: 'required', trigger: 'blur' }],
                lowestVersion: [{ required: true, message: 'required', trigger: 'blur' }],
                newestCode: [{ required: true, message: 'required', trigger: 'blur' }],
                newestVersion: [{ required: true, message: 'required', trigger: 'blur' }],
                title: [{ required: true, message: 'required', trigger: 'blur' }],
                content: [{ required: true, message: 'required', trigger: 'blur' }],
                directUrl: [{ required: true, message: 'required', trigger: 'blur' }],
                deleteflag: [{ required: true, message: 'required', trigger: 'blur' }],
            },
            temp: {
            }
        }
    },

    watch: {
        'row': {
            immediate: true, // vue首次赋值不触发watch的解决方法
            handler: function(nval){
                if (this.isModule) {
                    this.temp = Object.assign({}, this.row) // copy obj
                }
            }
        }
    },

    methods: {
        // 给外部调用，清空form
        resetTemp() {
            this.temp = {
                type: '',
                lowestCode: '',
                lowestVersion: '',
                newestCode: '',
                newestVersion: '',
                title: '',
                content: '',
                directUrl: '',
                deleteflag: '',
                id: undefined
            }
        },
        // 给外部调用，设置默认值，请根据实际情况修改id值
        defaultTemp() {
            this.temp = {
                deleteflag: 0 //设置默认值
            }
        },
        // 树表： 拿当前节点ID作为父节点ID，建立树关系
        setParentNodeId(id) {
            this.temp.tmptAppVersionId = id
        },
        // 给外部调用，插入记录
        createData() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    const param = Object.assign({}, this.temp)
                    createApi(param).then((r) => {
                        this.notify(r)
                        if (r.success) {
                            this.resetTemp()
                            // 从父组件调用此方法，进行回调下
                            if (this.isModule) {
                                this.$emit('createResult')
                            }
                        }
                    })
                }
            })
        },
        // 给外部调用，修改记录
        updateData() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    const param = { id:this.temp.id }
                    param.type = this.temp.type
                    param.lowestCode = this.temp.lowestCode
                    param.lowestVersion = this.temp.lowestVersion
                    param.newestCode = this.temp.newestCode
                    param.newestVersion = this.temp.newestVersion
                    param.title = this.temp.title
                    param.content = this.temp.content
                    param.directUrl = this.temp.directUrl
                    param.deleteflag = this.temp.deleteflag
                    updateApi(param).then((r) => {
                        this.notify(r)
                        if (r.success) {
                            this.resetTemp()
                            // 从父组件调用此方法，进行回调下
                            if (this.isModule) {
                                this.$emit('updateResult')
                            }
                        }
                    })
                }
            })
        },
        // 给外部调用，清除校验
        clearValidate() {
            this.$refs['dataForm'].clearValidate()
        }
    }

}
</script>
<style>

</style>
